package com.bnlr.lrparking.appui.paypwd;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.event.SetPayPasswordEvent;
import com.bnlr.lrparking.model.BaseArrayBean;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.view.ClearEditText;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

/**
 * 重置支付密码， 验证身份信息
 *
 * Created by hp on 2017/8/22.
 */

public class ForgetPwdAuthenticationActivity extends BaseActivity {

    private TextView tvNavTitle;
    private String card;
    private ClearEditText authentication_card;
    private TextView tv_authentication_btn;
    private String mima;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpwdauthentication);
        EventBus.getDefault().register(this);//
        Intent intent = getIntent();
        mima = intent.getStringExtra("mima");
        initView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void SuccessEvent(SetPayPasswordEvent event) {
        finish();
    }

    private void initView() {
        tvNavTitle = (TextView) findViewById(R.id.tv_nav_title);
        tvNavTitle.setText("填写证件号");
        authentication_card = (ClearEditText) findViewById(R.id.authentication_card);
        tv_authentication_btn = (TextView) findViewById(R.id.tv_authentication_btn);
        tv_authentication_btn.setBackgroundResource(R.drawable.seletor_login_btn);
        authentication_card.addTextChangedListener(this);

        tv_authentication_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (RegexUtils.isIDCard15(card) || RegexUtils.isIDCard18(card)) {
                    verifyID();
                } else {
                    ToastUtils.showShort("身份证格式不正确");
                }
            }
        });
    }

    private void verifyID() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "ValidIdCard");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("idCard", card);
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<BaseArrayBean>(this, true, BaseArrayBean.class) {
                        @Override
                        public void doWork(BaseArrayBean data, String code) {
                            if (TextUtils.equals("1", code)) {
                                Intent intent = new Intent(ForgetPwdAuthenticationActivity.this, PayPasswordActivity.class);
                                intent.putExtra("phonepwd", card);
                                intent.putExtra("mima", mima);
                                intent.putExtra("tag", "reset");//重置支付密码
                                startActivity(intent);
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (!TextUtils.equals("1", code)) {
                                    ToastUtils.showShort(obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        super.afterTextChanged(s);
        card = authentication_card.getText().toString();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
